import 'package:aqua_plunge/dao/broadcaster_dao.dart';
import 'package:aqua_plunge/model/followItem.dart';
import 'package:aqua_plunge/services/database_service.dart';
import 'package:aqua_plunge/util/toast_util.dart';
import 'package:aqua_plunge/widget/app_bar.dart';
import 'package:aqua_plunge/widget/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../controller/DatabaseListController.dart';
import '../controller/UserController.dart';

class FloolowingPage extends StatefulWidget {
  const FloolowingPage({super.key});

  @override
  State<FloolowingPage> createState() => _FloolowingPageState();
}

class _FloolowingPageState extends State<FloolowingPage> {

  final UserController _userController = Get.find<UserController>();
  final DatabaseController _databaseController = Get.find<DatabaseController>();
  bool loading = false;

  get _listview => Obx((){
    return Container(
      padding: const EdgeInsets.all(24),
      decoration: const BoxDecoration(
          image: DecorationImage(
              image: AssetImage('images/img_primary_bg.png'),
              fit: BoxFit.cover)),
      child: ListView.builder(
          itemCount: _userController.followingList.length,
          itemBuilder: (context, index) {
            return _buildListItem(_userController.followingList[index]);
          }),
    );
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar("Following List"),
      body: Stack(
        children: [
          _listview,
          Visibility(visible:loading,child: Center(child: CircularProgressIndicator(),))
        ],
      ),
    );
  }

  Widget _buildListItem(FollowItem item) {
    return Container(
        margin: const EdgeInsets.only(top: 10, bottom: 10),
        width: double.infinity,
        padding: const EdgeInsets.only(left: 12, right: 24),
        height: 80,
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage('images/list_item_bg.png'),
                fit: BoxFit.cover)),
        child: Row(
          children: [
            CustomAvatar(width: 48, height: 48, netPath: item.avatarUrl ?? ''),
            const SizedBox(width: 8),
            Expanded(
                child: FittedBox(
              fit: BoxFit.scaleDown,
              alignment: Alignment.centerLeft,
              child: Text(
                item.nickname ?? "",
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: const TextStyle(color: Colors.white, fontSize: 14),
              ),
            )),
            const SizedBox(width: 8),
            GestureDetector(
                onTap: () {
                  setState(() {
                    loading = true;
                  });
                  BroadCasterDao.unFriend(int.parse(item.userId!), () {
                      DatabaseService.instance.updateAuthorFriend(int.parse(item.userId!), false);
                      _userController.updateFollowBlock();
                      _databaseController.authors[item.userId]!.isFriend = false;
                      appToast.showTipLong('Unfollow Successfully');
                      setState(() {
                        loading = false;
                      });
                  });
                },
                child: Image.asset(
                  "images/icon_following.png",
                  width: 24,
                  height: 24,
                ))
          ],
        ));
  }

}
